package com.k2data.k2app.service;

import com.k2data.k2app.domain.po.v1.Contacts;
import com.k2data.k2app.domain.po.v1.RDeviceContacts;
import com.k2data.k2app.exception.K2ResponseException;
import com.k2data.k2app.mapper.ContactsMapper;
import com.k2data.k2app.mapper.RDeviceContactsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author lidong9144@163.com 17-3-20.
 */
@Service
public class ContactsService {

    @Autowired
    private ContactsMapper contactsMapper;
    @Autowired
    private RDeviceContactsMapper rDeviceContactsMapper;
    /**
     * 插入设备联系人
     *
     * @param po 实体
     * @return 插入 id
     * @throws K2ResponseException
     */
    @Transactional
    public Long insert(Contacts po,Long deviceId) throws K2ResponseException {
        Long id = contactsMapper.insertOfSelective(po);
        RDeviceContacts rDeviceContacts = new RDeviceContacts();
        rDeviceContacts.setDeviceId(deviceId);
        rDeviceContacts.setContactsId(id);
        rDeviceContactsMapper.insert(rDeviceContacts);
        return id;
    }

    @Transactional
    public int update(Contacts contacts) throws K2ResponseException {
        return contactsMapper.updateByPrimaryKeySelective(contacts);
    }

    @Transactional
    public int delete(Long id) throws K2ResponseException {
        return contactsMapper.deleteByPrimaryKey(id);
    }
}
